package com.mingtu.jgspatrol.activity;

import android.view.KeyEvent;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.king.keyboard.KingKeyboard;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.base.BaseBindingActivity;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.MyUtills;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.databinding.ActivityParkAddWhiteListBinding;
import com.mingtu.jgspatrol.utils.MyConstant;

import java.util.HashMap;

public class ParkAddWhiteListActivity extends BaseBindingActivity<ActivityParkAddWhiteListBinding> {
    private String plateNo;
    private String owner;
    private String phone;
    private String parkCode;
    private KingKeyboard kingKeyboard;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_park_add_white_list;
    }

    @Override
    protected void initView() {
        setModuleTitle("新增白名单");
        setDarkThemeStatusBar();


//        popupKeyboard = new PopupKeyboard(this);
//        popupKeyboard.attach(inputView, this);
//        popupKeyboard.getKeyboardEngine().setHideOKKey(true);
//        popupKeyboard.getKeyboardEngine().setLocalProvinceName("河南省");

        kingKeyboard = new KingKeyboard(this, binding.layoutKeyboard);
        kingKeyboard.register(binding.editCarNum, KingKeyboard.KeyboardType.LICENSE_PLATE);

        addClickListener(this, binding.button);
    }

    @Override
    protected void initData() {
        //全屏或沉浸式状态栏下软键盘遮挡输入框解决方法
        KeyboardUtils.fixAndroidBug5497(this);
    }

    @Override
    protected void getData() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.button:
                plateNo = MyUtills.getEditText(binding.editCarNum);
                owner = MyUtills.getEditText(binding.editName);
                phone = MyUtills.getEditText(binding.editTel);
                if (StringUtils.isEmpty(plateNo)) {
                    ToastUtils.showLong("请输入车牌号！");
                    return;
                }
                if (StringUtils.isEmpty(owner)) {
                    ToastUtils.showLong("请输入联系人！");
                    return;
                }

                if (StringUtils.isEmpty(phone)) {
                    ToastUtils.showLong("请输入手机号！");
                    return;
                }
                parkCode = binding.parkFilterView.getParkCode();

                if (StringUtils.isEmpty(parkCode)) {
                    ToastUtils.showLong("暂不支持此停车场！");
                    return;
                }

                addWhiteList();
                break;
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        kingKeyboard.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        kingKeyboard.onDestroy();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (kingKeyboard.isShow()) {
                kingKeyboard.hide();
            } else {
                finish();
            }
        }
        return super.onKeyUp(keyCode, event);
    }

    /**
     * 添加白名单
     */
    private void addWhiteList() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("owner", owner);
        hashMap.put("parkCode", parkCode);
        hashMap.put("phoneNo", phone);
        hashMap.put("plateNo", plateNo);
        MyUtills.removeMapNull(hashMap);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_ADD_WHITE_LIST)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(ActivityUtils.getTopActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            JSONObject jsonObject = JSON.parseObject(result);
                            String msg = jsonObject.getString("msg");
                            if (msg.equals(Constant.RESULT_SUCCESS)) {
                                ToastUtils.showLong("添加成功！");
                                //刷新列表
                                setResult(200);
                                finish();
                            }
                        }
                    }
                });

    }
}